#!/bin/bash
#本文件用于创建通道、加入节点及设置锚节点

# imports  
. scripts/envVar.sh
. scripts/utils.sh

CHANNEL_NAME="channel1"
DELAY="$2"
MAX_RETRY="$3"
VERBOSE="$4"
: ${CHANNEL_NAME:="channel1"}
: ${MAX_RETRY:="5"}
: ${VERBOSE:="false"}

: ${CONTAINER_CLI:="docker"}
: ${CONTAINER_CLI_COMPOSE:="${CONTAINER_CLI}-compose"}
infoln "Using ${CONTAINER_CLI} and ${CONTAINER_CLI_COMPOSE}"

if [ ! -d "channel-artifacts" ]; then
	mkdir channel-artifacts
fi

verifyResult() {
  if [ $1 -ne 0 ]; then
    fatalln "$2"
  fi
}

createChannelGenesisBlock() {
	which configtxgen
	if [ "$?" -ne 0 ]; then
		fatalln "configtxgen tool not found."
	fi
	set -x
	configtxgen -profile Channel1Genesis -outputBlock ./channel-artifacts/${CHANNEL_NAME}.block -channelID $CHANNEL_NAME
	res=$?
	{ set +x; } 2>/dev/null
  verifyResult $res "Failed to generate channel configuration transaction..."
}

createChannel() {
	setGlobals 1
	
	local rc=1
	local COUNTER=1
	while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ] ; do
		sleep $DELAY
		set -x
		osnadmin channel join --channelID $CHANNEL_NAME --config-block ./channel-artifacts/${CHANNEL_NAME}.block -o localhost:7053 --ca-file "$ORDERER_CA" --client-cert "$ORDERER_ADMIN_TLS_SIGN_CERT" --client-key "$ORDERER_ADMIN_TLS_PRIVATE_KEY" >&log.txt
		res=$?
		{ set +x; } 2>/dev/null
		let rc=$res
		COUNTER=$(expr $COUNTER + 1)
	done
	cat log.txt
	verifyResult $res "Channel creation failed"
}

#将peer0.org1加入channel1
joinChannelPeer0() {
  	FABRIC_CFG_PATH=$PWD/../config/
	setGlobals 1
	local rc=1
	local COUNTER=1
	#此过程可能时间较长
	while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ] ; do
    sleep $DELAY
    set -x
    peer channel join -b $BLOCKFILE >&log.txt
    res=$?
    { set +x; } 2>/dev/null
		let rc=$res
		COUNTER=$(expr $COUNTER + 1)
	done
	cat log.txt
	verifyResult $res "After $MAX_RETRY attempts, peer0.org1 has failed to join channel '$CHANNEL_NAME' "
}

#将peer1.org1加入channel1
joinChannelPeer1() {
  	FABRIC_CFG_PATH=$PWD/../config/
	setGlobals 1
	export CORE_PEER_ADDRESS=localhost:7058

	local rc=1
	local COUNTER=1
	#此过程可能时间较长
	while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ] ; do
    sleep $DELAY
    set -x
    peer channel join -b $BLOCKFILE >&log.txt
    res=$?
    { set +x; } 2>/dev/null
		let rc=$res
		COUNTER=$(expr $COUNTER + 1)
	done
	cat log.txt
	verifyResult $res "After $MAX_RETRY attempts, peer1.org1 has failed to join channel '$CHANNEL_NAME' "
}

#将peer0.org2加入channel1
joinChannelPeer0Org2() {
  	FABRIC_CFG_PATH=$PWD/../config/
	setGlobals 2

	local rc=1
	local COUNTER=1
	#此过程可能时间较长
	while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ] ; do
    sleep $DELAY
    set -x
    peer channel join -b $BLOCKFILE >&log.txt
    res=$?
    { set +x; } 2>/dev/null
		let rc=$res
		COUNTER=$(expr $COUNTER + 1)
	done
	cat log.txt
	verifyResult $res "After $MAX_RETRY attempts, peer0.org2 has failed to join channel '$CHANNEL_NAME' "
}

#将peer1.org2加入channel1
joinChannelPeer1Org2() {
  	FABRIC_CFG_PATH=$PWD/../config/
	setGlobals 2
	export CORE_PEER_ADDRESS=localhost:7063

	local rc=1
	local COUNTER=1
	#此过程可能时间较长
	while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ] ; do
    sleep $DELAY
    set -x
    peer channel join -b $BLOCKFILE >&log.txt
    res=$?
    { set +x; } 2>/dev/null
		let rc=$res
		COUNTER=$(expr $COUNTER + 1)
	done
	cat log.txt
	verifyResult $res "After $MAX_RETRY attempts, peer1.org2 has failed to join channel '$CHANNEL_NAME' "
}

setAnchorPeer() {
	${CONTAINER_CLI} exec cli ./scripts/createChannel1/setAnchorPeerOrg1.sh 
	${CONTAINER_CLI} exec cli ./scripts/createChannel1/setAnchorPeerOrg2.sh 
}

FABRIC_CFG_PATH=${PWD}/configtx

#生成创世区块
infoln "Generating channel genesis block '${CHANNEL_NAME}.block'"
createChannelGenesisBlock

FABRIC_CFG_PATH=$PWD/../config/
BLOCKFILE="./channel-artifacts/${CHANNEL_NAME}.block"

#创建通道
infoln "Creating channel ${CHANNEL_NAME}"
createChannel
successln "Channel '$CHANNEL_NAME' created"

#将3个peer节点都加入channelA
infoln "Joining peer0.org1 to the channelA..."
joinChannelPeer0
infoln "Joining peer1.org1 to the channelA..."
joinChannelPeer1
infoln "Joining peer0.org2 to the channelA..."
joinChannelPeer0Org2
infoln "Joining peer1.org2 to the channelA..."
joinChannelPeer1Org2

#设置org1的锚节点
infoln "Setting anchor peer for org1 and Org2..."
setAnchorPeer

successln "Channel '$CHANNEL_NAME' joined"
